/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.app.topic.conversation.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ctc.wstx.util.StringUtil;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.data.record.entity.AppDataRecord;
import com.jeesite.modules.app.data.record.service.AppDataRecordService;
import com.jeesite.modules.app.topic.comment.entity.AppTopicComment;
import com.jeesite.modules.app.topic.comment.service.AppTopicCommentService;
import com.jeesite.modules.app.topic.conversation.entity.ApptopicOfConversation;
import com.jeesite.modules.app.topic.conversation.service.ApptopicOfConversationService;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.utils.DictUtils;

/**
 * 社群管理Controller
 * @author zpp
 * @version 2020-02-03
 */
@Controller
@RequestMapping(value = "/app/topic/conversation/")
public class ApptopicOfConversationController{

	@Autowired
	private ApptopicOfConversationService apptopicOfConversationService;
	@Autowired
	private AppTopicCommentService appTopicCommentService;
	@Autowired
	private AppDataRecordService appDataRecordService;
	/**
	 * 查询话题
	 */
	@RequestMapping(value="listData")
	public void list(ApptopicOfConversation entity,HttpServletRequest request,HttpServletResponse response){
		List<ApptopicOfConversation> findList = apptopicOfConversationService.findList(entity);
		JSONArray parseArray = JSONObject.parseArray(JsonMapper.toJson(findList));
		AppController.renderSuccessObject(response, parseArray, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value="findPage")
	public void findPage(ApptopicOfConversation entity,HttpServletRequest request,HttpServletResponse response){
		entity.setPage(new Page<>(request, response));
		Page<ApptopicOfConversation> findPage = apptopicOfConversationService.findPage(entity);
		for(ApptopicOfConversation model: findPage.getList()){
			String topicImg = model.getTopicImg();
			if(StringUtils.isNoneBlank(topicImg)){
				String[] split = topicImg.split(",");
				List<String> topicImages=new ArrayList<>();
				for(String key:split){
					if(StringUtils.isNotBlank(key)){
						topicImages.add(key);
					}
				}
				model.setTopicImages(topicImages);
			}
		}
		JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(findPage));
		AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value="myfindPage")
	public void myfindPage(ApptopicOfConversation entity,HttpServletRequest request,HttpServletResponse response){
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		entity.setCreateBy(userId);
		entity.setPage(new Page<>(request, response));
		Page<ApptopicOfConversation> findPage = apptopicOfConversationService.findPage(entity);
		JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(findPage));
		AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
	}
	/**
	 * 社群页接口
	 * @param entity
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="pageData")
	public void pageData(ApptopicOfConversation entity,HttpServletRequest request,HttpServletResponse response){
		//1.查询类型和分类
		List<DictData> hotOrRecomm=DictUtils.getDictList("t_hot_or_recomm");
		List<DictData> conversation = DictUtils.getDictList("t_topic_conversation");
		JSONArray horJson=JSONObject.parseArray(JsonMapper.toJson(hotOrRecomm));
		JSONArray csJson=JSONObject.parseArray(JsonMapper.toJson(conversation));
		
		//分页查询
		entity.setPage(new Page<>(request, response));
		Page<ApptopicOfConversation> findPage = apptopicOfConversationService.findPage(entity);
		JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(findPage));
		JSONObject json=new JSONObject();
		json.put("hotOrRecomm", horJson);
		json.put("conversation", csJson);		
		json.put("topicConversate", parseObject);
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value="detail")
	public void detail(ApptopicOfConversation entity,HttpServletRequest request,HttpServletResponse response){
		List<ApptopicOfConversation> findList = apptopicOfConversationService.findList(entity);
		AppTopicComment comment=new AppTopicComment();
		for(ApptopicOfConversation model:findList){
			String id=model.getId();
			comment.setTopicId(id);
			List<AppTopicComment> findList2 = appTopicCommentService.findList(comment);
			for(AppTopicComment data:findList2){
				AppDataRecord adr=new AppDataRecord();
				adr.setDataId(data.getId());
				adr.setUserId(data.getCommentator());
				Long findCount = appDataRecordService.findCount(adr);
				if(findCount!=null&&findCount>0){
					data.setLikeYesNo(1);
				}
			}
			model.setCommentList(findList2);
			String dictValues = model.getItemize();
			if(StringUtils.isNotBlank(dictValues)){
				String dictLabels = DictUtils.getDictLabels("t_topic_conversation", dictValues, "");
				String[] split = dictLabels.split(",");
				model.setItemizes(split);
			}
			String topicImg = model.getTopicImg();
			if(StringUtils.isNoneBlank(topicImg)){
				String[] split = topicImg.split(",");
				List<String> topicImages=new ArrayList<>();
				for(String key:split){
					if(StringUtils.isNotBlank(key)){
						topicImages.add(key);
					}
				}
				model.setTopicImages(topicImages);
			}
		}		
		JSONArray parseArray = JSONObject.parseArray(JsonMapper.toJson(findList));
		AppController.renderSuccessObject(response, parseArray, MessageCode.SUCCESS_ACK);
	}
	/**
	 * 添加话题
	 */
	@RequestMapping(value="add")
	public void add(ApptopicOfConversation entity,HttpServletRequest request,HttpServletResponse response){
		String topicTitle = entity.getTopicTitle();
		if(StringUtils.isBlank(topicTitle)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "标题不存在");
			return;
		}
		String topicContent = entity.getTopicContent();
        if(StringUtils.isBlank(topicContent)){
        	AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "内容不存在");
			return;
		}
		String itemize = entity.getItemize();
        if(StringUtils.isBlank(itemize)){
        	AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "分类不存在");
			return;
		}
        String userId=request.getParameter("userId");
        if(StringUtils.isBlank(userId)){
        	AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "发布人不存在");
			return;
        }
        entity.setCreateBy(userId);
        entity.setLookNum(0L);
        entity.setCommentNum(0L);
        entity.setShareNum(0L);
        apptopicOfConversationService.save(entity);
        AppController.renderSuccessObject(response, "", MessageCode.SUCCESS_ACK);
	}
}